<script setup lang="ts">
import { computed } from 'vue'
import AddRecord from './AddRecord.vue'
import moment from 'moment';


const props = defineProps<{
  records: Array<{
    id: number
    name: string
    type: string
    date: string
    amount: number
  }>
  showAddForm: { [key: string]: boolean }
  startDate: string
  endDate: string
}>()

const emit = defineEmits(['toggle-add-form', 'add-success'])

// 获取周数
const getWeekNumber = (date: Date) => {
  // 使用 moment.js 来解析传入的日期
  const momentDate = moment(date);
  // 获取本年度的第几周
  return momentDate.isoWeek();  // isoWeek() 返回的是ISO标准的周数
}

// 生成周键
const generateWeekKey = (date: Date) => {
  return `${date.getFullYear()}-W${getWeekNumber(date)}`
}

// 按周分组记录
const weeklyRecords = computed(() => {
  const groups: {[key: string]: typeof props.records} = {}
  const start = new Date(props.startDate)
  const end = new Date(props.endDate)

  // 初始化每周的空数组
  for (let date = new Date(start); date <= end; date.setDate(date.getDate() + 7)) {
    const weekKey = generateWeekKey(date)
    if (!groups[weekKey]) {
      groups[weekKey] = []
    }
  }

  // 填充实际记录
  props.records.forEach(record => {
    const date = new Date(record.date)
    const weekKey = generateWeekKey(date)
    if (!groups[weekKey]) {
      groups[weekKey] = []
    }
    groups[weekKey].push(record)
  })

  // 获取所有周键并倒序排列
  const sortedWeekKeys = Object.keys(groups).sort((a, b) => b.localeCompare(a))

  // 创建一个新的对象来存储排序后的结果
  const sortedGroups: {[key: string]: typeof props.records} = {}
  sortedWeekKeys.forEach(weekKey => {
    sortedGroups[weekKey] = groups[weekKey]
  })

  return sortedGroups
})

// 计算周统计数据
const getWeeklyStats = (records: typeof props.records) => {
  return {
    income: records.filter(r => r.type === '收入').reduce((sum, r) => sum + r.amount, 0),
    expense: records.filter(r => r.type === '支出').reduce((sum, r) => sum + r.amount, 0)
  }
}
</script>

<template>
  <div class="week-view">
    <div v-for="(weekRecords, weekKey) in weeklyRecords" :key="weekKey" class="week-group">
      <div class="week-header">
        <h2>{{ weekKey }}</h2>
        <div class="weekly-summary">
          <span class="income-text">周收入: ¥{{ getWeeklyStats(weekRecords).income }}</span>
          <span class="expense-text">周支出: ¥{{ getWeeklyStats(weekRecords).expense }}</span>
        </div>
      </div>

      <div class="records">
        <div class="record-header">
          <span class="col date">日期</span>
          <span class="col name">名称</span>
          <span class="col type">类型</span>
          <span class="col amount">金额</span>
        </div>
        
        <!-- 如果该周没有记录，显示空状态 -->
        <div v-if="weekRecords.length === 0" class="empty-state">
          <p>该周暂无记录</p>
        </div>
        
        <!-- 有记录时显示记录列表 -->
        <template v-else>
          <div v-for="record in weekRecords" :key="record.id" class="record-item">
            <span class="col date">{{ record.date }}</span>
            <span class="col name">{{ record.name }}</span>
            <span class="col type">
              <span class="type-tag" :class="record.type === '收入' ? 'income' : 'expense'">
                <i :class="record.type === '收入' ? 'fa fa-plus-circle' : 'fa fa-minus-circle'"></i>
                <span class="type-text">{{ record.type }}</span>
              </span>
            </span>
            <span class="col amount" :class="record.type === '收入' ? 'income' : 'expense'">
              {{ record.type === '收入' ? '+' : '-' }}¥{{ record.amount }}
            </span>
          </div>
        </template>
      </div>
    </div>
  </div>
</template>

<style scoped>
.week-view {
  width: 100%;
}

.week-group {
  margin-bottom: 48px;
  background: white;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.week-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  padding-bottom: 12px;
  border-bottom: 2px solid #e8e8e8;
}

.week-header h2 {
  margin: 0;
  color: #1890ff;
  font-size: 20px;
}

.weekly-summary {
  display: flex;
  gap: 24px;
  font-size: 16px;
  font-weight: 500;
}

.records {
  margin-top: 16px;
}

.record-header {
  display: flex;
  padding: 8px 16px;
  background: #fafafa;
  border-radius: 4px;
  margin-bottom: 8px;
  font-weight: 500;
  color: #666;
}

.record-item {
  display: flex;
  padding: 12px 16px;
  border-bottom: 1px solid #f0f0f0;
}

.record-item:last-child {
  border-bottom: none;
}

.col {
  flex: 1;
  display: flex;
  align-items: center;
}

.col.date {
  flex: 0.8;
  color: #666;
}

.col.name {
  flex: 2;
}

.col.type {
  justify-content: center;
}

.col.amount {
  justify-content: flex-end;
  font-weight: 500;
}

.type-tag {
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.type-text {
  margin-left: 2px;
  color: #333;
}

.type-tag.income {
  background: rgba(82, 196, 26, 0.1);
  border: 1px solid #52c41a;
}

.type-tag.expense {
  background: rgba(245, 34, 45, 0.1);
  border: 1px solid #f5222d;
}

.income-text {
  color: #52c41a;
}

.expense-text {
  color: #f5222d;
}

.income {
  color: #52c41a;
}

.expense {
  color: #f5222d;
}

.empty-state {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 24px;
  color: #999;
  font-size: 14px;
  background: #fafafa;
  border-radius: 4px;
}

.empty-state p {
  margin: 0;
}
</style> 